﻿#ifndef JTCPSERVER_H
#define JTCPSERVER_H

#include "JTcp.h"
#include "JThread.h"

#include <QMap>
#include <QMutex>
#include <QMutexLocker>

class JTcpServer : public JTcp
{
public:
    JTcpServer();
    ~JTcpServer();

    int                         create(unsigned short nPort, int nTimeout = 1);
    void                        close();

    void                        startDataThread();              // 启动数据处理线程
    void                        stopDataThread();               // 终止数据处理线程
    int                         processData(SOCKET s);          // 处理数据

    int                         sendData(char *szData, int nLen);
    int                         recvData(char *szData, int nMaxLen, long sec, long msec);
    int                         recvPackData(SOCKET s);

public:
    SOCKET                      _serverSocket;
    QMutex                      _mutex;
    QMap<SOCKET, CLIENT_INFO*>  _clientInfoMap;
    int                         _nTimeout;                      // 超时时间，单位为秒

    char                        _recvBuf[BUF_SIZE];             // 接收缓冲区

private:

public:
    JThread *                   _pDataThread;                   // 数据处理线程
};

#endif // JTCPSERVER_H
